<?php
/**
 * Smithy PHP library
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * Smithy is a free Zend Framework based PHP library.
 *
 * @category   Smithy
 * @package    Smithy_Model
 * @subpackage RecordSet
 * @copyright  Copyright (c) 2008-2009 Kovács Tamás
 * @license    New BSD License
 * @version    $Id: Abstract.php ktamas $
 */

/**
 * A basic recordset for models.
 *
 * @author      Kovács Tamás
 * @category    Smithy
 * @package     Smithy_Model
 * @subpackage  RecordSet
 */
abstract class Smithy_Model_RecordSet_Abstract implements Iterator, Countable
{
    /**
     * The model gateway.
     *
     * @var Smithy_Model_Gateway_Abstract
     */
    protected $_gateway = null;

    /**
     * The resultset array.
     *
     * @var Iterator|Countable|ArrayAccess
     */
    protected $_resultSet = array();

    /**
     * The type of the value objects, contained by the recordset.
     *
     * @var string
     */
    protected $_valueObjectClass = 'Smithy_Model_Value_Abstract';

    /**
     * How many items are there in the recordset.
     *
     * @var int
     */
    protected $_count = null;


    /**
     * Constructor
     *
     * @param array|Zend_Db_Table_Rowset_Abstract $result
     * @param Smithy_Model_Gateway_Abstract $gateway
     * @return void
     */
    public function __construct ($result, $gateway = null)
    {
        $this->_resultSet = $result;
        $this->_gateway = $gateway;

        $this->init();
    }

    /**
     * Initialize gateway.
     *
     * @return void
     */
    public function init()
    {}

    /**
     * Returns the gateway object.
     *
     * @return Smithy_Model_Gateway_Abstract
     */
    public function getGateway()
    {
        return $this->_gateway;
    }

    /**
     * Returns the current element.
     *
     * @see Iterator::current()
     * @return null|Smithy_Model_Value_Abstract
     */
    public function current ()
    {
        if ($this->_resultSet instanceof Iterator) {
            $key = $this->_resultSet->key();
        } else {
            $key = key($this->_resultSet);
        }

        $current = $this->_resultSet[$key];

        if (!$current instanceof $this->_valueObjectClass) {
            $current = $this->createValueObject($current);
            //$this->_resultSet[$key] = $current;
        }

        return $current;
    }

    /**
     *
     * @see Iterator::key()
     */
    public function key ()
    {
        return $this->_resultSet->key();
    }

    /**
     *
     * @see Iterator::next()
     */
    public function next ()
    {
        return $this->_resultSet->next();
    }

    /**
     *
     * @see Iterator::rewind()
     */
    public function rewind ()
    {
        return $this->_resultSet->rewind();
    }

    /**
     *
     * @see Iterator::valid()
     */
    public function valid ()
    {
        return $this->_resultSet->valid();
    }

    /**
     *
     * @see Countable::count()
     */
    public function count ()
    {
        if (null === $this->_count) {
            $this->_count = $this->_resultSet->count();
        }

        return $this->_count;
    }

    /**
     * Creates a value object using the gateway.
     *
     * @param array|Zend_Db_Table_Row_Abstract $data
     */
    public abstract function createValueObject($data);
}
